﻿$(function () {
    $("#dengluzhuce").on('click', function () {
        window.location.href = "/CreateAccount/CreateAccount";
    });
    $('#tijiao').on('click', function () {
        $(this).attr("disabled", "disabled");
        var name = $("#user").val(),
            email = $("#email").val(),
            password = $("#Pass").val(),
            pass = $("#pass").val();
        $(this).ajaxSubmit({
            url: '/CreateAccount/CreatePostAccount',
            type: 'POST',
            data: {
                //'Name': $("#user").val(),
                //'Email': $("#email").val(),
                //'PassWord': $("#Pass").val(),
                'Name': name,
                'Email': email,
                'PassWord': password,
            },
            beforeSubmit: function (formData, jqForm, options) {
                if (pass == password & pass.length>=6 & name.length >= 2 & email.indexOf("@")>0) {
                    return true;
                }
                else if(name.length<2) {
                    alertwin("数据不合法","用户名不能少于2位！！");
                    $("#user").val("").focus();
                    return false;
                }
                else if (email.indexOf("@") <= 0) {
                    alertwin("数据不合法", "邮箱格式错误！！");
                    
                    $("#email").val("").focus();
                    return false;
                }
                else if (pass != password) {
                    alertwin("数据不合法", "密码有误！！");
                  
                    $("#Pass").val("").focus();
                    $("#pass").val("").focus();
                    return false;
                }
                alertwin("未知错误", "请重新输入！！");
                return false;
            },
            success: function (responseText, statusText) {
                if (responseText == "注册成功"){
                    alertwin("将要重定向", "注册成功请登录！！");
                window.location.href = "/Logn/Index";
                }
                else {
                    alertwin("注册失败，请重试！！");
                    window.location.href = "/CreateAccount/CreateAccount";
                }
                },
            error: function (event, errorText, errorType) {
                alertwin("注册失败",请重试);
                window.location.href = "/CreateAccount/CreateAccount";
            },
        });
    });
    //登录
    $('#denglu').on('click', function () {
        $(this).attr("disabled", "disabled");
        var name = $("#uesr").val(),
            pass = $("#pass").val();
        $(this).ajaxSubmit({
            url: '/Logn/Logn',
            type: 'POST',
            data: {
                //'Name': $("#user").val(),
                //'Email': $("#email").val(),
                //'PassWord': $("#Pass").val(),
                'Name':name,
                'PassWord':pass,
            },
            beforeSubmit: function (formData, jqForm, options) {
                //alert(name);
                //alert(pass);
                if (pass.length >= 6&name.length>= 2) {
                    return true;
                }
                else if (name.length < 2) {
                    alertwin("输入错误","账号不能少于2位");
                    $("#user").val("").focus();
                    return false;
                }
                else if (pass.length<6) {
                    alertwin("输入错误","密码不能少于6位，请重新输入！！")
                    $("#pass").val("").focus();
                    return false;
                }
                alertwin("未知错误", "未知错误")
                return false;
            },
            success: function (responseText, statusText) {
                if (responseText == "账号异常") {
                    alertwin("服务端异常","登录失败!!");
                    window.location.href = "/Logn/Index";
                }
                else {
                    alertwin("登陆成功！！","将要重定向");
                    window.location.href = "/Home/Index/" + responseText;
                }
            },
            error: function (event, errorText, errorType) {
                alertwin("登录失败", "登录失败");
                window.location.href = "/Logn/Index";
            },
        });
    });
    //邮箱自动补全
    $('#email').autocomplete({
        delay: 0,
        autoFocus: true,
        source: function (request, response) {
            //获取用户输入的内容
            //alert(request.term);
            //绑定数据源的
            //response(['aa', 'aaaa', 'aaaaaa', 'bb']);

            var hosts = ['qq.com', '163.com', '263.com', 'sina.com.cn', 'gmail.com', 'hotmail.com'],
                term = request.term,		//获取用户输入的内容
                name = term,				//邮箱的用户名
                host = '',					//邮箱的域名
                ix = term.indexOf('@'),		//@的位置
                result = [];				//最终呈现的邮箱列表


            result.push(term);

            //当有@的时候，重新分别用户名和域名
            if (ix > -1) {
                name = term.slice(0, ix);
                host = term.slice(ix + 1);
            }

            if (name) {
                //如果用户已经输入@和后面的域名，
                //那么就找到相关的域名提示，比如bnbbs@1，就提示bnbbs@163.com
                //如果用户还没有输入@或后面的域名，
                //那么就把所有的域名都提示出来

                var findedHosts = (host ? $.grep(hosts, function (value, index) {
                    return value.indexOf(host) > -1
                }) : hosts),
                    findedResult = $.map(findedHosts, function (value, index) {
                        return name + '@' + value;
                    });

                result = result.concat(findedResult);
            }

            response(result);
        },
    });
    browe();
  
    function browe() {
        var Sys = {};
        var ua = navigator.userAgent.toLowerCase();
        if (window.ActiveXObject) {
            Sys.ie = ua.match(/msie ([\d.]+)/)[1]
            if (Sys.ie <= 7) {
               
                location.href = "/Brow";
            }
        }
    }


    function alertwin(tilele,content) {
        $.alert({
            title: tilele,
            content: content,
            confirmButton: '确认',
            columnClass: 'col-md-4 col-md-offset-4',
           
        });
    }
});